﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace Business
{
    //only for ViewMasterPlan
    public class MyProject : Project
    {
        public string ProgramName { get; set; }
        public List<string> ListEmployeeCode { get; set; }
        public string Field_CS { get; set; }
    }

    //only for ViewMasterPlan
    public partial class Project
    {
        public IEnumerable<SimpleEmployee> ListEmployeeCodeIE { get; set; }
        public string StatusConvert
        {
            get
            {
                Common.STATUS_CODE.ROW_STATUS foo = (Common.STATUS_CODE.ROW_STATUS)Status;
                return Common.EnumUtils.stringValueOf(foo);
            }
        }
    }

    public partial class Project
    {
        //Init MasterPlanCode K00001
        public static string CreateCode()
        {
            return null;
        }
        
        // Optional K00006

        //GetProjectByID
        public static void GetProjectByProjectID(ref Project project, long projectID, bool getAll = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                if (getAll)
                {
                    project = context.Projects.Single(s => s.ID == projectID);
                }
                else
                {
                    project = context.Projects.Single(s => s.ID == projectID && s.Status == 1);
                }
            }
            catch
            {
                return;
            }
            finally
            {
                context = null;
            }
        }

        //AddNew MasterPlan K00004
        //public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(ref Project pj, Common.UserInfo userinfo)
        //{
        //    DataClassesDataContext db = null;
        //    try
        //    {
        //        db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
        //        Project newPJ = new Project();

        //        newPJ.ProgramID = pj.ProgramID;
        //        newPJ.ProjectCode = pj.ProjectCode;
        //        newPJ.ProjectName = pj.ProjectName;
        //        newPJ.DefaultMargin = pj.DefaultMargin;
        //        newPJ.StartDate = pj.StartDate;
        //        newPJ.EndDate = pj.EndDate;
        //        newPJ.CreatedBy = userinfo.UserID;
        //        newPJ.CreatedDateTime = DateTime.Now;
        //        newPJ.RowVersion = 1;
        //        newPJ.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
        //        db.Projects.InsertOnSubmit(newPJ);

        //        db.SubmitChanges();

        //        pj = newPJ;

        //        return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
        //    }
        //    catch
        //    {
        //        return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
        //    }
        //    finally
        //    {
        //        db = null;
        //    }
        //}

        //AddNew MasterPlan K00004
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE Update(ref Project newPJ, long projectID, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                Project pj = db.Projects.Single(s => s.ID == projectID);
                pj.ProgramID = newPJ.ProgramID;
                pj.ProjectCode = newPJ.ProjectCode;
                pj.ProjectName = newPJ.ProjectName;
                pj.DefaultMargin = newPJ.DefaultMargin;
                pj.StartDate = newPJ.StartDate;
                pj.EndDate = newPJ.EndDate;
                pj.LastUpdatedDateTime = DateTime.Now;
                pj.LastUpdatedBy = userinfo.UserID;
                pj.RowVersion += 1;

                db.SubmitChanges();

                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
            }
            catch
            {
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }

        public static void GetProjectNameForCombo(ref List<ComboItem> list)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                foreach (var item in context.Projects)
                {
                    ComboItem add = new ComboItem(item.ID, item.ProjectName);
                    list.Add(add);
                }
                //if (isAllOptions)
                //   list.Insert(0, new ComboItem("000000", "Tất cả"));
            }
            catch
            {
                list = null;
                return;
            }
            finally
            {
                context = null;
            }
        }

        // GetProjectByUserID
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetProjectByUserID(ref string messageError, ref List<MyProject> list, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
               db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
               var query = from dt in db.Projects
                           where dt.CreatedBy == userinfo.UserID && dt.Status == 1
                           select new MyProject
                           {
                               ID = dt.ID,
                               ProgramID = dt.ProgramID,
                               ProjectCode = dt.ProjectCode,
                               ProjectName = dt.ProjectName,
                               DefaultMargin = dt.DefaultMargin,
                               StartDate = dt.StartDate,
                               EndDate = dt.EndDate,
                               Status = dt.Status,
                               CreatedBy = dt.CreatedBy,
                               CreatedDateTime = dt.CreatedDateTime,
                               RowVersion = dt.RowVersion,

                               ProgramName = db.Programs
                                .Where(p => p.ID == dt.ProgramID)
                                .Select(s => s.ProgramName)
                                .First(),

                               ListEmployeeCodeIE = db.ProgramEmployees
                                .Where(p => p.ProgramID == dt.ProgramID)
                                .Select(p => new SimpleEmployee { EmployeeCode = p.EmployeeCode, EmployeeName = p.Employee.EmployeeName }),
                           };
               list = query.ToList();
               foreach (var item in list)
               {
                   foreach (var cs in item.ListEmployeeCodeIE)
                   {
                       if (item.Field_CS == null)
                           item.Field_CS += cs.EmployeeCode;
                       else
                           item.Field_CS += "," + cs.EmployeeCode;
                   }
               }

                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }
    }
}
